package com.iworkh.spring.aop.anno.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * Logger切面
 *
 * @author: iworkh-沐雨云楼
 * @date: 2019-12-19
 */
@Component
@Aspect
@Order(2)
public class LoggerAspect {

    @Pointcut("execution(* com.iworkh.spring.aop.anno..*(..))")
    public void commPointCut() {
    }

    @Before(value = "commPointCut()")
    private void beforeMethod(JoinPoint joinPoint) {
        System.out.println("LoggerAspect...before--" + joinPoint.getSignature().getName());
    }

    @After(value = "LoggerAspect.commPointCut()")
    private void afterMethod() {
        System.out.println("LoggerAspect...after");
    }

    @AfterReturning(value = "com.iworkh.spring.aop.anno.aspect.LoggerAspect.commPointCut()", returning = "result")
    private void afterReturning(Object result) {
        System.out.println("LoggerAspect...afterReturning--" + result);
    }

    @AfterThrowing(value = "execution(* com.iworkh.spring.aop.anno..*(..))", throwing = "ex")
    private void afterThrowing(Exception ex) {
        System.out.println("LoggerAspect...afterThrowing--" + ex);
    }

    // @Around(value = "execution(* com.iworkh.spring.aop.anno..*(..))")
    // private Object around(ProceedingJoinPoint pjp) {
    //
    //     Object result = null;
    //     try {
    //         System.out.println("LoggerAspect...around..before");
    //         result = pjp.proceed();
    //     } catch (Throwable throwable) {
    //         System.out.println("LoggerAspect...around..afterThrowing--"+throwable.getMessage());
    //         throwable.printStackTrace();
    //     }finally {
    //         System.out.println("LoggerAspect...around..after");
    //     }
    //     System.out.println("LoggerAspect...around..afterReturning--"+result);
    //     return result;
    // }
}
